package com.nursinghome.admin.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nursinghome.admin.service.MedicalStaffService;
import com.nursinghome.bean.MedicalStaff;
import com.nursinghome.bean.RespBean;
import com.nursinghome.exception.NurisingHomeException;
import com.nursinghome.group.MedicalUpdateGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.groups.Default;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/admin/medical")
public class MedicalStaffController {
    @Autowired
    private MedicalStaffService medicalStaffService;
    @Value("${pageSize}")
    private Integer pageSize;
    //获取到所有的护工
    @GetMapping
    public RespBean selectAll(Integer pageNum, String mname, String mphone, String musername) {
        //如何pageNum为空默认查询第一页
        if (pageNum == null){
            pageNum = 1;
        }
        PageHelper.startPage(pageNum, 4);
        List<MedicalStaff> medicalStaffs = medicalStaffService.selectAll(mname,mphone,musername);
        PageInfo<MedicalStaff> elderlyPageInfo = new PageInfo<>(medicalStaffs);
        return RespBean.ok("",elderlyPageInfo);
    }
    //新增护工
    @PostMapping
    public RespBean insertMedical(@RequestBody @Validated({Default.class, MedicalUpdateGroup.class}) MedicalStaff medicalStaff) throws Exception {
        if (medicalStaffService.addMedical(medicalStaff)) {
            return RespBean.ok("新增成功");
        }
        return RespBean.error("新增失败");
    }
    //修改护工
    @PutMapping
    public RespBean updateMedical(@RequestBody @Validated({Default.class, MedicalUpdateGroup.class}) MedicalStaff medicalStaff) throws NurisingHomeException {
        if (medicalStaffService.updateMedical(medicalStaff)) {
            return RespBean.ok("修改成功");
        }
        return RespBean.error("修改失败");
    }
    //删除护工
    @DeleteMapping("/{mid}")
    public RespBean delMedical(@PathVariable("mid") Integer mid) throws NurisingHomeException {
        if (medicalStaffService.deleteMedical(mid)) {
            return RespBean.ok("删除成功");
        }
        return RespBean.error("删除失败");
    }
    //回显护工数据
    @GetMapping("/{mid}")
    public RespBean selectByMid(@PathVariable("mid") Integer mid) {
        MedicalStaff medicalStaff = medicalStaffService.selectByMid(mid);
        return RespBean.ok("",medicalStaff);
    }

}
